package com.ecoolex.union.pay.retailer.api.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.union.pay.retailer.entity.UnionPayAutoReceiveCouponConfig;
import com.ecoolex.union.pay.retailer.pojo.ManulReceiveUnionPayCouponDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayAutoReceiveCouponConfigService;
import com.ecoolex.union.pay.retailer.ws.UnionPayCouponService;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "银联自动领券配置")
public class UnionPayAutoReceiveCouponConfigGraphql {

	@Reference
    private UnionPayAutoReceiveCouponConfigService service;
	
	@Reference
	private UnionPayCouponService unionPayCouponService;
	
	@QueryField(field = "unionPayAutoReceiveCouponConfigList", description = "自动领取银联优惠券")
	public Boolean query(@NonNull String channelId, @NonNull ManulReceiveUnionPayCouponDTO coupon, ConsumerMemberAuthority user){
		user.assertLogin();
		List<UnionPayAutoReceiveCouponConfig> list = service.queryByChannelId(channelId);
		if(Check.notNullOrEmpty(list)) {
			for (UnionPayAutoReceiveCouponConfig config : list) {
				coupon.setCouponCode(config.getCouponCode());
				unionPayCouponService.manulReceiveUnionPayCoupon(coupon, user.userId(), user.userName());
			}
		}
		return true;
	}
}
